package com.ws.test;

import java.util.Scanner;

/**
 * @author 王硕
 * @version jdk21
 * @className test
 * @description 幸运抽奖系统
 * @date 2024/11/1 20:42
 */
public class test {

    private static Scanner scanner = new Scanner(System.in);
    // 用户账号存储
    private static String[] usernames = new String[100];
    // 用户密码存储
    private static String[] passwords = new String[100];
    // 用户卡号存储
    private static int[] cardNumbers = new int[100];
    // 已注册的用户数量
    private static int userCount = 0;
    // 当前登录用户索引
    private static int loggedUserIndex = -1;

    public static void main(String[] args) {
        while (true) {
            System.out.println("************欢迎进入幸运抽奖系统************");
            System.out.println("1:注册");
            System.out.println("2:登录");
            System.out.println("3:切换账号");
            System.out.println("4:抽奖");
            System.out.println("****************************************");
            System.out.println("请选择功能:");
            int inputnumber = scanner.nextInt();
            scanner.nextLine();
            switch (inputnumber) {
                case 1:
                    register();
                    break;
                case 2:
                    login();
                    break;
                case 3:
                    switchUser();
                    break;
                case 4:
                    prizeDraw();
                    break;
                default:
                    System.out.println("你输入有误，请输入1、2、3或4。");
                    break;
            }
            if (!continueOperation()) {
                break;
            }
        }
        scanner.close();
    }

    /**
     * 判断用户是否继续操作
     */
    private static boolean continueOperation() {
        System.out.println("继续y/退出n:");
        String ipt = scanner.next();
        if ("y".equalsIgnoreCase(ipt)) {
            return true;
        } else if ("n".equalsIgnoreCase(ipt)) {
            return false;
        } else {
            System.out.println("你输入有误，请输入y或n。");
            return continueOperation();
        }
    }

    /**
     * 注册功能
     */
    private static void register() {
        System.out.println("请选择功能-->注册");
        System.out.println("请输入用户名:");
        String username = scanner.next();
        // 检查用户名是否已存在
        for (int i = 0; i < userCount; i++) {
            if (usernames[i] != null && usernames[i].equals(username)) {
                System.out.println("用户名已存在，请重新输入。");
                return;
            }
        }
        System.out.println("请输入密码:");
        String password = scanner.next();

        // 检查是否需要扩容
        if (userCount >= usernames.length) {
            // 扩容操作
            String[] newUsernames = new String[usernames.length * 2];
            String[] newPasswords = new String[passwords.length * 2];
            int[] newCardNumbers = new int[cardNumbers.length * 2];
            // 复制旧数组内容到新数组
            for (int i = 0; i < userCount; i++) {
                newUsernames[i] = usernames[i];
                newPasswords[i] = passwords[i];
                newCardNumbers[i] = cardNumbers[i];
            }
            // 更新引用
            usernames = newUsernames;
            passwords = newPasswords;
            cardNumbers = newCardNumbers;
        }

        // 添加新用户
        usernames[userCount] = username;
        passwords[userCount] = password;
        cardNumbers[userCount] = generateRandomNumber();
        System.out.println("注册成功，您的卡号是: " + cardNumbers[userCount]);
        userCount++;
    }

    /**
     * 登录功能
     */
    private static void login() {
        System.out.println("请选择功能-->登录");
        if (loggedUserIndex != -1) {
            System.out.println("您已经登录，欢迎" + usernames[loggedUserIndex] + "！");
            System.out.println("要切换账号，请选择'切换账号'功能。");
            return;
        }
        System.out.println("请输入用户名:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();
        for (int i = 0; i < userCount; i++) {
            if (usernames[i] != null && usernames[i].equals(username) && passwords[i].equals(password)) {
                System.out.println("登录成功，欢迎" + username + "！");
                loggedUserIndex = i; // 记录登录用户的索引
                return;
            }
        }
        System.out.println("用户名或密码错误。");
    }

    /**
     * 切换用户功能
     */
    private static void switchUser() {
        if (loggedUserIndex == -1) {
            System.out.println("您当前没有登录任何账号。");
            return;
        }
        System.out.println("您已切换账号。");
        loggedUserIndex = -1; // 用户登出
    }

    /**
     * 抽奖功能
     */
    private static void prizeDraw() {
        if (loggedUserIndex == -1) {
            System.out.println("您还未登录或未切换到有效账号。");
            return;
        }
        System.out.println("请选择功能-->抽奖");
        int number1 = generateRandomNumber();
        int number2 = generateRandomNumber();
        int number3 = generateRandomNumber();
        int number4 = generateRandomNumber();
        System.out.println("幸运号码: " + number1 + ", " + number2 + ", " + number3 + ", " + number4);
        int userCardNumber = cardNumbers[loggedUserIndex];
        System.out.println("你的卡号是" + userCardNumber);
        if (userCardNumber == number1 || userCardNumber == number2 || userCardNumber == number3 || userCardNumber == number4) {
            System.out.println("恭喜你中奖了");
        } else {
            System.out.println("很可惜你没有中奖");
        }
    }

    /**
     * 生成一个1000到9999之间的随机数
     */
    private static int generateRandomNumber() {
        return (int) (Math.random() * 9000) + 1000;
    }
}